<template>
  <div class="upload-container">
    <div v-if="resultUrl">
      <el-link target="_blank" :href="resultUrl" :underline="false">
        <el-button size="mini" type="primary">点击下载</el-button>
      </el-link>
    </div>
    <el-upload class="upload-demo" drag :auto-upload="false" :action="''" :file-list="fileList"
      :before-upload="beforeUpload" :on-change="onChange" multiple>
      <i class="el-icon-upload"></i>
      <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
    </el-upload>
  </div>
</template>

<script>
import { getToken } from '@/utils/auth'
import { getconfigUrl } from '@/api/sys/user'

export default {
  name: 'UploadFile',

  data() {
    return {
      fileList: [],
      imageUrl: '',
      uploadImageUrl: process.env.VUE_APP_BASE_API + '/oss/attachment/upload',
      uploadData: { base_path: this.basePath },
      myHeaders: { access_token: getToken() },
      imageExtension: ['bmp', 'jpg', 'jpeg', 'png', 'gif'],
      extensions: [],
      uploadLoading: false,
      resultUrl: undefined,
      waringLabel: '只能上传' + this.extension + '文件，且不超过' + this.limit + 'M',
    }
  },

  methods: {
    fun(item) {
      this.fileList = []
      getconfigUrl({
        params: {
          userId: item.id,
        },
      }).then((res) => {
        if (res.data.code == 0) {
          this.fileList.push({
            name: res.data.data.fileName,
            url: res.data.data.url,
          })
        }
      })
    },
    handleChange(file) {
      this.onChange(file.raw)
    },
    onChange(file) {
      const reader = new FileReader()
      reader.readAsDataURL(file.raw)
      reader.onload = (e) => {
        console.log(e.target)
        this.imageUrl = e.target.result
        console.log(this.imageUrl, '111111111111');
        this.$emit('fileurlWatch', this.imageUrl)
        this.$emit('filename', file.raw)
      }
    },

    beforeUpload(file) {
      // 扩展名判断
      const fileExtension = this.getFileExtension(file.name)
      if (fileExtension === '') {
        this.$message.error('文件必须有扩展名')
        return false
      }

      if (this.extension !== '') {
        const isFileExtension = this.extensions.includes(fileExtension)
        if (!isFileExtension) {
          this.$message.error('上传文件只能是' + this.extension + '格式!')
          return false
        }
      }
      // 判断文件大小
      const isLimitSize = file.size / 1024 / 1024 < this.limit
      if (!isLimitSize) {
        this.$message.error('上传头像图片大小不能超过 ' + this.limit + 'MB!')
      }
      if (isLimitSize) {
        this.uploadLoading = true
      }
      return isLimitSize
    },
  },
}
</script>

<style>
.uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.uploader .el-upload:hover {
  border-color: #409eff;
}

.uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.waring-label {
  font-size: 14px;
  color: #9b9d07;
}
</style>
